package com.youlei.newgraduateproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlei.newgraduateproject.entity.SysCustomer;
import com.youlei.newgraduateproject.entity.SysOrdersOrderitem;
import com.youlei.newgraduateproject.service.*;
import com.youlei.newgraduateproject.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author youlei
 * @version 1.0
 * @Date 2021-05-13-19:28
 * @Description 所有Controller继承的通用Controller
 */
public class BaseController {

    @Autowired
    HttpServletRequest req;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysMenuService sysMenuService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    SysRoleMenuService sysRoleMenuService;

    @Autowired
    SysOrdersService sysOrdersService;

    @Autowired
    SysOrderitemService sysOrderitemService;

    @Autowired
    SysOrdersOrderitemService sysOrdersOrderitemService ;

    @Autowired
    SysCustomerService sysCustomerService;


    /**
     *  获取页码
     * @return
     */
    public Page getPage() {
        //设置默认的当前页码和一页的数量
        int current = ServletRequestUtils.getIntParameter(req, "current", 1);
        int size = ServletRequestUtils.getIntParameter(req, "size", 5);

        return new Page(current, size);
    }


    /**
     * @Author youlei
     * @Description 根据orderId得到orderitemIds
     * @Date 15:36 2021/6/2
     * @Param: [orderId]
     * @return: java.util.List<java.lang.Long>
     **/
    public List<Long> getOrderitemIdsByOrderId(Long orderId) {
        //根据orderID查询OrdersOrderitem中间表,得到orderitemIds
        List<SysOrdersOrderitem> ordersOrderitems = sysOrdersOrderitemService
                .list(new QueryWrapper<SysOrdersOrderitem>()
                        .eq("order_id", orderId));
        return ordersOrderitems
                .stream()
                .map(p -> p.getOrderitemId())
                .collect(Collectors.toList());
    }
    public String getCustomerNameByCustomerId(Long customerId){
        SysCustomer customer = sysCustomerService
                .getOne(new QueryWrapper<SysCustomer>()
                        .select("customer_name")
                        .eq("customer_id", customerId));
        return customer.getCustomerName();

    }

}
